/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.st.entity.StCheckStandard;
import com.jeesite.modules.st.dao.StCheckStandardDao;
import com.jeesite.modules.st.entity.StCheckStandardDetail;
import com.jeesite.modules.st.dao.StCheckStandardDetailDao;

/**
 * 物料检验标准Service
 * @author HOHD
 * @version 2020-12-16
 */
@Service
@Transactional(readOnly=true)
public class StCheckStandardService extends CrudService<StCheckStandardDao, StCheckStandard> {
	
	@Autowired
	private StCheckStandardDetailDao stCheckStandardDetailDao;
	
	/**
	 * 获取单条数据
	 * @param stCheckStandard
	 * @return
	 */
	@Override
	public StCheckStandard get(StCheckStandard stCheckStandard) {
		StCheckStandard entity = super.get(stCheckStandard);
		if (entity != null){
			StCheckStandardDetail stCheckStandardDetail = new StCheckStandardDetail(entity);
			stCheckStandardDetail.setStatus(StCheckStandardDetail.STATUS_NORMAL);
			entity.setStCheckStandardDetailList(stCheckStandardDetailDao.findList(stCheckStandardDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param stCheckStandard 查询条件
	 * @param stCheckStandard.page 分页对象
	 * @return
	 */
	@Override
	public Page<StCheckStandard> findPage(StCheckStandard stCheckStandard) {
		return super.findPage(stCheckStandard);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param stCheckStandard
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(StCheckStandard stCheckStandard) {
		super.save(stCheckStandard);
		// 保存 StCheckStandard子表
		for (StCheckStandardDetail stCheckStandardDetail : stCheckStandard.getStCheckStandardDetailList()){
			if (!StCheckStandardDetail.STATUS_DELETE.equals(stCheckStandardDetail.getStatus())){
				stCheckStandardDetail.setParentId(stCheckStandard);
				if (stCheckStandardDetail.getIsNewRecord()){
					stCheckStandardDetailDao.insert(stCheckStandardDetail);
				}else{
					stCheckStandardDetailDao.update(stCheckStandardDetail);
				}
			}else{
				stCheckStandardDetailDao.delete(stCheckStandardDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param stCheckStandard
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(StCheckStandard stCheckStandard) {
		super.updateStatus(stCheckStandard);
	}
	
	/**
	 * 删除数据
	 * @param stCheckStandard
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(StCheckStandard stCheckStandard) {
		super.delete(stCheckStandard);
		StCheckStandardDetail stCheckStandardDetail = new StCheckStandardDetail();
		stCheckStandardDetail.setParentId(stCheckStandard);
		stCheckStandardDetailDao.deleteByEntity(stCheckStandardDetail);
	}
	
}